package ca.jacobk.foodspin;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import ca.jacobk.fileio.OptionsManager;
import ca.jacobk.fileio.WheelOption;

public class HelloListView extends ListActivity {
	/**
	 * Reference to the options manager
	 */
	private OptionsManager optionsManager;
	
	/**
	 * List of WheelOption values.
	 */
	List<WheelOption> options;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		OptionsManager.context = getApplicationContext();
		optionsManager = OptionsManager.getInstance();
		
		options = optionsManager.getAllOptions();

		setListAdapter(new ArrayAdapter<WheelOption>(this, R.layout.list_item, options));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		      // When clicked, show a toast with the TextView text
		    	Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
		    			Toast.LENGTH_SHORT).show();
		      
		    	//This is the code that makes it able to switch between screens
		      	Intent myIntent = null;
				if ( ((TextView)view).getText().equals("Subway") ) {
					myIntent = new Intent(view.getContext(), FoodSpinActivity.class);
				}
				if (myIntent != null) {
					startActivity(myIntent);
				}
		    }
		});
		
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				boolean retVal = false;
				Intent myIntent = null;
				if ( ((TextView)view).getText().equals("Subway") ) {
					myIntent = new Intent(view.getContext(), FoodSpinActivity.class);
				}
				
				startActivity(myIntent);
				return retVal;
			}
			
		});
	}
}
